<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <script>
      class Father {
        constructor(x, y) {
          this.x = x;
          this.y = y;
          // 打印使用new实例化 对象
          console.log(new.target, "father");
          if (new.target === Father) {
            throw new Error("只能通过子类实例化");
          }
        }
        sum() {
          console.log(this.x + this.y);
        }
        study() {
          console.log("好好学习");
        }
      }

      class Son extends Father {
        constructor(x, y) {
          super(x, y);
          this.x = x;
          this.y = y;
          console.log(new.target, "son");
          // 调用父类的方法需要使用super

          // super.study() //调用父类的函数
        }
        mutiple() {
          console.log(this.x * this.y);
        }
      }
      var son2 = new Son(7, 9);
      // const son3 = new Father()报错  只能通过子类实例化
      son2.sum()
      son2.mutiple();
      // son3.mutiple()
    </script>
  </body>
</html>
